package com.seeyon.apps.brwh.kit;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.Strings;
import com.seeyon.ctp.workflow.bean.params.SeeyonBPMHandParam;
import com.seeyon.ctp.workflow.bean.params.SeeyonBPMHandParam4Start;
import com.seeyon.ctp.workflow.service.api.InstApiService;
import com.seeyon.ctp.workflow.vo.SeeyonBPMHandResult;
import org.apache.commons.logging.Log;

import java.util.*;

public class FormKit {
    private static final Log log = CtpLogFactory.getLog(FormKit.class);
    private static OrgManager orgManager = (OrgManager)AppContext.getBean("orgManager");
    private static final InstApiService instApiService = (InstApiService)AppContext.getBean("instApiService");

    public static HashMap<String,Object> startForm(Map<String, Object> params) throws BusinessException {
        Map<String, Object> ret = null;
        HashMap<String,Object> resultMap = new HashMap();
        String checkRet = checkNecessaryParams(params, "appName");
        if (Strings.isNotBlank(checkRet)) {
            resultMap.put("code", 500);
            resultMap.put("data", null);
            resultMap.put("msg", checkRet);
        } else {
            SeeyonBPMHandParam4Start startParam = new SeeyonBPMHandParam4Start();
            initParam(params, startParam);
            String templateCode = startParam.getStringParam("templateCode");
            if (Strings.isBlank(templateCode)) {
                String returnMsg = "No parameter or the value is NULL :templateCode";
                resultMap.put("code", 500);
                resultMap.put("data", null);
                resultMap.put("msg", returnMsg);
            } else {
                log.info("******BPM startProcess begign******");
                log.info("appName:=" + startParam.getAppName());
                log.info("templateCode:=" + templateCode);
                log.info("draft:=" + startParam.getStringParam("draft"));
                SeeyonBPMHandResult handRet = instApiService.transStartProcess(startParam);
                log.info("******BPM startProcess end******");
                if (!handRet.isHandResult()) {
                    resultMap.put("code", handRet.getErrorCodeIntValue());
                    resultMap.put("msg", handRet.getErrorMsg());
                    resultMap.put("data", null);
                } else {
                    ret = handRet.getDatas();
                    resultMap.put("data", ret);
                    resultMap.put("code", 200);
                    resultMap.put("msg", "");
                }
            }
        }
        return resultMap;
    }



    private static String checkNecessaryParams(Map<String, Object> params, String... keys) {
        String ret = null;
        StringBuilder missKey = new StringBuilder();
        if (keys != null && keys.length != 0) {
            if (params != null) {
                String[] var5 = keys;
                int var6 = keys.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    String key = var5[var7];
                    if (!params.containsKey(key) || params.get(key) == null || params.get(key) instanceof String && Strings.isBlank(params.get(key).toString())) {
                        missKey.append(key).append(",");
                    }
                }
            }

            if (missKey.length() > 0) {
                ret = "No parameter or the value is NULL :" + missKey.toString();
            }

            return ret;
        } else {
            return ret;
        }
    }

    private static void initParam(Map<String, Object> reqDatas, SeeyonBPMHandParam param) {
        if (reqDatas != null && param != null) {
            param.setAppName((String)reqDatas.get("appName"));
            User user = AppContext.getCurrentUser();

            try {
                V3xOrgMember member = getOrgManager().getMemberById(user.getId());
                param.setCurrentUser(member);
            } catch (BusinessException var6) {
                log.error("rest接口初始化人员信息失败", var6);
            }

            param.setAppData((Map)reqDatas.get("data"));
        }
    }


    private static OrgManager getOrgManager() {
        if (null == orgManager) {
            orgManager = (OrgManager)AppContext.getBean("orgManager");
        }

        return orgManager;
    }
}
